package org.darkforest.common.util;

/**
 * Created by Justin on 15/1/18.
 */


/**
 * ClassName: TypeUtil
 * Description:
 * author: xuxiaoqiang xuxiaoqiang@baidu.com
 * date: 14-5-12 下午4:57
 */
public class TypeUtil {

    private TypeUtil() {

    }

    /**
     * Convert string to long, if not success return default number
     *
     * @param str str
     * @param defaultNumber default
     *
     * @return long
     */
    public static long parseLong(String str, long defaultNumber) {
        if (str == null) {
            return defaultNumber;
        }
        try {
            return Long.parseLong(str);
        } catch (NumberFormatException ex) {
            return defaultNumber;
        }
    }

    /**
     * Convert string to int, if not success return default number
     *
     * @param str str
     * @param defaultNumber default number
     *
     * @return int
     */
    public static int parseInt(String str, int defaultNumber) {
        if (str == null) {
            return defaultNumber;
        }
        try {
            return Integer.parseInt(str);
        } catch (NumberFormatException ex) {
            return defaultNumber;
        }
    }

    /**
     * Convert string to double, if not success return default number
     *
     * @param str str
     * @param defaultNumber default number
     *
     * @return double
     */
    public static double parseDouble(String str, double defaultNumber) {
        if (str == null) {
            return defaultNumber;
        }
        try {
            return Double.parseDouble(str);
        } catch (NumberFormatException ex) {
            return defaultNumber;
        }
    }

    /**
     *
     * @param obj1 obj1
     * @param obj2 obj2
     * @return equal
     */
    public static boolean equal(Object obj1, Object obj2) {
        boolean obj1Null = obj1 == null;
        boolean obj2Null = obj2 == null;

        if (obj1Null) {
            return obj2Null || obj2.equals(obj1);
        } else {
            return obj1.equals(obj2);
        }
    }
}
